package com.ymatou.iapi.sellerquery.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrdersByIdsBean;
import com.ymatou.query.seller.trading.api.parameter.Seller.Common;
import com.ymatou.query.seller.trading.api.parameter.Seller.GetSellerOrdersByIdsResponse;
import com.ymatou.query.seller.trading.api.parameter.info.SellerOrderInfo;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 根据多个订单号查询 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetSellerOrdersByIds {
	private static GetSellerOrdersByIdsBean getsellerordersbyidsBean;
	private static com.ymatou.query.seller.trading.api.service.GetSellerOrdersByIdsCall getsellerordersbyidsCall;
//	private static com.ymatou.iapi.trading.sellerquery.service.GetSellerOrdersByIdsCall getsellerordersbyidsCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据多个订单号查询");
	}

	@Before
	public void caseUp() {
		getsellerordersbyidsBean = new GetSellerOrdersByIdsBean();
		getsellerordersbyidsCall = new com.ymatou.query.seller.trading.api.service.GetSellerOrdersByIdsCall();
//		getsellerordersbyidsCall = new com.ymatou.iapi.trading.sellerquery.service.GetSellerOrdersByIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerordersbyidsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerOrdersByIdsResponse expectedResponse = new GetSellerOrdersByIdsResponse();
		ConstructExpectedResponse(expectedResponse);
		GetSellerOrdersByIdsResponse actualResponse = getsellerordersbyidsCall
				.getSellerOrdersByIdsResponse();
		if (expectedResponse.getOrders() == null) {
			DataUtil.verifyEquals(null, actualResponse.getOrders(),
					"验证期望的Orders=null");
		} else {
			DataUtil.verifyEquals(expectedResponse.getOrders().size(),
					actualResponse.getOrders().size(), "验证Orders的数量");
			boolean flag = expectedResponse.getOrders().size() == actualResponse
					.getOrders().size();
			if (flag) {
				for (int i = 0; i < expectedResponse.getOrders().size(); i++) {
					SellerOrderInfo expect = expectedResponse.getOrders()
							.get(i);
					long expectId = expect.getBasic().getId();
//					DataUtil.verifyEquals(expect.getBasic().getID(),
//							actualResponse.getOrders().get(i).getBasic()
//									.getID(), "验证Orders中第" + (i + 1) + "条记录的排序");
					boolean exists = actualResponse.getOrders().stream()
							.anyMatch(x -> x.getBasic().getId() == expectId);
					DataUtil.verifyEquals(true, exists, "验证Orders中存在Basic.ID=["
							+ expectId + "]的订单记录");
					if (exists) {
						SellerOrderInfo actual = actualResponse.getOrders()
								.stream()
								.filter(x -> x.getBasic().getId() == expectId)
								.collect(Collectors.toList()).get(0);
						Ts_GetSellerOrders.verifySellerOrderInfo(expect,
								actual, expectId);
					}
				}
			}
		}
	}

	private void ConstructExpectedResponse(GetSellerOrdersByIdsResponse response) {
		List<SellerOrderInfo> orders = Common.GetSellerOrders(DataUtil.Integerr
				.GetValueOrDefault(getsellerordersbyidsBean.getSellerId()),
				getsellerordersbyidsBean.getOrderids());
		response.setOrders(orders);
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_001() {
		Logger.start(true, "查询多个M订单");
		try {
			Logger.comment("准备前置数据");
			List<Integer> orderids = new ArrayList<Integer>();
			int sellerId = 0;
			OrderCallService orderService = new OrderCallService();
			for (int i = 0; i < 3; i++) {
				PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

				int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
				 sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
				int userId =placeOrderResp.getUserId();         
				int tradingId = placeOrderResp.getTradingId();

				BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
				// 买家付款
				orderService.orderPayRequest(userId, totalPrice, tradingId);
				// 接单
				orderService.acceptOrder(orderId, sellerId);
				// 商家发货
				orderService.dispatched(orderId, sellerId);
				// 确认收货
				orderService.confirmReceive(userId, orderId);
				orderids.add(orderId);
			}
			Logger.comment("准备前置数据结束");
			getsellerordersbyidsBean.setSellerId(sellerId);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordersbyidsCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_002() {
		Logger.start(true, "查询多个C订单");
		try {
			Logger.comment("准备前置数据");
			List<Integer> orderids = new ArrayList<Integer>();
			int sellerId = 0;
			OrderCallService orderService = new OrderCallService();
			for (int i = 0; i < 3; i++) {
				// 创建订单
				Map map = orderService.appSaveOrder();
				int orderId = (Integer) map.get("orderId");
				sellerId = (Integer) map.get("sellerId");
				int userId = (Integer) map.get("userId");
				int tradingId = (Integer) map.get("tradingId");
				DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
				BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
				// 买家付款
				orderService.orderPayRequest(userId, totalPrice, tradingId);
				// 接单
				orderService.acceptOrder(orderId, sellerId);
				// 卖家发起补款
				int price = 1 + new Random().nextInt(50);
				;
				BigDecimal payAmount = new BigDecimal(df.format(price));
				String postPayId = orderService.applyForPostPay(orderId,
						sellerId, payAmount);
				// 买家补款
				orderService.orderPostPayRequest(userId, payAmount, orderId);
				orderids.add(orderId);
			}
			Logger.comment("准备前置数据结束");
			getsellerordersbyidsBean.setSellerId(sellerId);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordersbyidsCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_003() {
		Logger.start(false, "卖家id为null");
		try {
			List<Integer> orderids = new ArrayList<Integer>();

			orderids.add(105808892);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerordersbyidsCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_004() {
		Logger.start(false, "卖家id为0");
		try {
			List<Integer> orderids = new ArrayList<Integer>();

			orderids.add(105808892);

			getsellerordersbyidsBean.setSellerId(0);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerordersbyidsCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_005() {
		Logger.start(false, "订单id数量为null");
		try {
			List<Integer> orderids = new ArrayList<Integer>();

			/*
			 * orderids.add(105808892);
			 * 
			 * orderids.add(105808898); orderids.add(100157353);
			 * orderids.add(100157361);
			 */
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getsellerordersbyidsBean.setSellerId(sellerId);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerordersbyidsCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_006() {
		Logger.start(false, "订单id数量超过200个");
		int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
		try {
			List<Integer> orderids = new ArrayList<Integer>();

			for (int i = 0; i < 201; i++) {
				orderids.add(i);
			}
			getsellerordersbyidsBean.setSellerId(sellerId);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();
			Logger.verifyEquals(ECode.ERROR400,
					getsellerordersbyidsCall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrdersByIds_007() {
		Logger.start(true, "查询多个M订单, 有退货信息");
		try {
			Logger.comment("准备前置数据");
			List<Integer> orderids = new ArrayList<Integer>();
			int sellerId = 0;
			OrderCallService orderService = new OrderCallService();
			for (int i = 0; i < 3; i++) {
				PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

				int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
				 sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
				int userId =placeOrderResp.getUserId();         
				int tradingId = placeOrderResp.getTradingId();

				BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
				// 买家付款
				orderService.orderPayRequest(userId, totalPrice, tradingId);
				// 接单
				orderService.acceptOrder(orderId, sellerId);
				// 商家发货
				orderService.dispatched(orderId, sellerId);
				// 确认收货
				orderService.confirmReceive(userId, orderId);
				orderids.add(orderId);
				// 退货
				orderService.salesRefundSuccessRequestInJava(orderId);
				Thread.sleep(2000l);
			}
			Logger.comment("准备前置数据结束");
			getsellerordersbyidsBean.setSellerId(sellerId);
			getsellerordersbyidsBean.setOrderids(orderids);
			getsellerordersbyidsCall.setData(getsellerordersbyidsBean);
			getsellerordersbyidsCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getsellerordersbyidsCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}